import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
from game_stats import GameStats
from button import Button
from scoreboard import Scoreborad
#主函数
def run_game():
    #创建一个窗体
    pygame.init()
    #创建配置对象
    ai_settings =  Settings()
    #给窗体设置宽高
    screen =pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    #设置标题
    pygame.display.set_caption("外星人入侵")
    #创建飞船对象
    ship = Ship(ai_settings,screen)
    #创建子弹数组 空数组
    bullets = Group()
    #创建一行外星人
    aliens = Group()
    gf.create_fleet(ai_settings,screen,ship,aliens)
    #计分类
    stats = GameStats(ai_settings)
    play_button = Button(ai_settings,screen,"Play Game")
    #得分版
    sb = Scoreborad(ai_settings,screen,stats)
    #开始游戏的主循环
    while True:
      #1秒钟刷新
      #pygame.time.Clock().tick(60)
      # 100
      #窗体的事件处理  FPS 1/60
      #check_events(ai_settings,screen,stats,play_button,ship,aliens, bullets):
      gf.check_events(ai_settings,screen,stats,sb,play_button,ship,aliens, bullets)
      if stats.game_active==True:
          #更新飞船位置
          ship.update()
          #更新子弹位置
          gf.update_bulltes(ai_settings,screen,stats,sb,ship,aliens,bullets)
          #更新外星人的位置
          gf.update_aliens(ai_settings,stats,sb,screen,ship,aliens,bullets)

      #游戏画面更新
      gf.update_screen(ai_settings,screen,stats,ship,bullets,aliens,play_button,sb)

run_game()